"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 等待延迟过程中failover
Create At   : 2021/03/02
@zou_jialiang0505328126
Description :
    1.设置延迟为5min
    2.创建表
    3.等待30s，停止主节点，主降备
    4.新主节点查询
    5.重启原主节点
    6.原主节点查询
Expect      :
    1.设置成功
    2.创建成功
    3.切换成功
    4.新主节点可查询数据
    5.重启成功
    6.原主节点可查询数据
History     :
"""

import unittest
import time
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class RecoveryDelay(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case253.py 开始执行------')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.db_standby_node =  Node(node='Standby1Root')
        self.comshsta = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.tb_name = 'tb_252'

        self.log.info('=====安装ntp服务=======')
        install_cmd = "yum install -y ntp && " \
                      "systemctl enable ntpd && systemctl start ntpd"
        result = self.db_standby_node.sh(install_cmd).result()
        self.log.info(result)

        self.log.info('------同步集群时间--------')
        current = self.db_primary_root_node.sh(
            "date \"+%m/%d/%Y %H:%M:%S\"").result()
        self.log.info(current)
        datecmd = f'date -s "{current}"'
        self.log.info(datecmd)
        result = self.db_standby_node.sh(datecmd).result()
        self.log.info(result)

    def test_recoverydelay(self):
        self.log.info('-----设置recovery_min_apply_delay=5min----')
        result = self.commonshpri.executeGsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'recovery_min_apply_delay=5min')
        self.assertTrue(result)

        self.log.info('--------创建表-------')
        sql = f"drop table if exists {self.tb_name};" \
            f"create table {self.tb_name}(i int, s char(10));"
        result = self.commonshpri.executDbSql(sql)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, result)

        self.log.info('----------等待30s，停止主节点，主降备--------------')
        time.sleep(30)
        result = self.commonshpri.executeGsctl(
            'stop', self.constant.GS_CTL_STOP_SUCCESS_MSG)
        self.assertTrue(result)
        result = self.comshsta.executeGsctl(
            'failover', self.constant.FAILOVER_SUCCESS_MSG)
        self.assertTrue(result)
        result = self.comshsta.exec_refresh_conf()
        self.assertTrue(result)

        self.log.info('----------等待15s查询---------------')
        time.sleep(15)
        sql = f"select sysdate;select * from {self.tb_name};"
        result = self.comshsta.executDbSql(sql)
        self.log.info(result)
        self.assertIn('(0 rows)', result)

        self.log.info('----------重启原主节点--------------')
        result = self.commonshpri.executeGsctl(
            'start', self.constant.RESTART_SUCCESS_MSG, '-M standby')
        self.assertTrue(result)

        self.log.info('----------原主节点查询---------------')
        sql = f"select sysdate;select * from {self.tb_name};"
        result = self.comshsta.executDbSql(sql)
        self.log.info(result)
        self.assertIn('(0 rows)', result)

    def tearDown(self):
        self.log.info('-----------------------------环境清理--------------------')
        self.log.info('-------------还原recovery_min_apply_delay----------')
        result = self.commonshpri.executeGsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'recovery_min_apply_delay=0')
        self.log.info(result)

        self.log.info('--------还原集群-------')
        self.commonshpri.executeGsctl(
            'start', self.constant.GS_CTL_STOP_SUCCESS_MSG, '-M standby')
        self.commonshpri.executeGsctl(
            'switchover', self.constant.SWITCHOVER_SUCCESS_MSG)
        self.commonshpri.exec_refresh_conf()

        time.sleep(5)
        self.log.info('--------删除表-------')
        sql = f"drop table if exists {self.tb_name};"
        result = self.commonshpri.executDbSql(sql)
        self.log.info(result)
        self.log.info('---Opengauss_Reliability_Dbsys_Case253.py 执行结束-------')